<template>
  <div class="webReg">
    <div class="reg">
      <div class="form">
        <div>
          <h3 :class="noLogin == true ? 'active' : ''">{{ loginStr }}</h3>
        </div>
        <div>
          <p>邮箱</p>
          <input autofocus="autofocus" v-model="userEmail" type="text" />
        </div>
        <div>
          <p>密码</p>
          <input v-model="userPassword" type="password" />
        </div>
        <div class="autoLogin">
          <input v-model="isCheck" type="checkbox" />
          <i>7天内自动登录</i> |
          <router-link to="/home/webreset">找回密码</router-link>
        </div>
        <div>
          <input @click="myLogin" type="submit" value="登录" />
        </div>
      </div>
      <div class="isHave">
        <div class="have">
          <p>还没有帐号？</p>
          <router-link to="/home/reg">马上注册</router-link>
        </div>
      </div>
      <transition
        :duration="{ enter: 1000, leave: 1000 }"
        enter-to-class="animate__animated animate__fadeIn"
        enter-active-class="animate__animated animate__fadeIn"
        leave-to-class="animate__animated animate__fadeOut"
        leave-active-class="animate__animated animate__fadeOut"
      >
        <div class="myTip" v-if="isTip">
          <div class="tip">
            <p>登录成功,正在跳转</p>
            <div class="img-box">
              <img src="../assets/images/login.gif" alt="" />
            </div>
          </div>
        </div>
      </transition>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  name: "webReg",
  data() {
    return {
      vcode: 1023,
      isVcode: true,
      userEmail: "",
      userPassword: "",
      userMsg: "",
      isTip: false,
      timer: null,
      yy: "",
      mm: "",
      dd: "",
      h: "",
      m: "",
      s: "",
      loginStr: "",
      isCheck: false,
    };
  },
  methods: {
    myLogin() {
      if (this.userEmail.length == 0 || this.userPassword.length == 0) {
        alert("邮箱或密码输入有误");
        return false;
      }
      let useremail = this.$store.state.userEmail;
      let userpassword = this.$store.state.userPassword;
      let _data = JSON.parse(localStorage.getItem("userMsg"));
      // console.log(_data);
      if (_data == null) {
        alert("该邮箱未注册，请注册");
        this.$router.push({ path: "/home/reg" });
        return false;
      }
      if (
        this.userEmail == this.userMsg.email &&
        this.userPassword == this.userMsg.password
      ) {
        this.isTip = true;
        this.timer = setTimeout(() => {
          this.$router.push({ path: "/home/diary" });
          let obj = JSON.parse(localStorage.getItem("userMsg"));
          this.$store.state.userEmail = obj.email;
          this.$store.state.userPassword = obj.password;
          this.$store.state.userName = obj.name;

          // 设置时间
          let timeObj = this.$store.state.timeObj;
          // console.log(timeObj);
          timeObj.year = this.yy;
          timeObj.month = this.mm;
          timeObj.day = this.dd;
          timeObj.hour = this.h;
          timeObj.minute = this.m;
          timeObj.weekend = this.s;

          this.$store.state.timeObj = timeObj;

          this.$store.state.isLogin = true;
          this.isTip = false;
          clearTimeout(this.timer);
        }, 2000);
      } else {
        alert("邮箱或密码输入有误");
        this.userPassword = "";
        return false;
      }

      // 7天内自动登录
      if (this.isCheck == true) {
        let curTime = new Date().getTime();
        let ms = 24 * 60 * 60 * 1000 * 7;
        let futureTime = curTime + ms;

        let dt = new Date();
        dt.setTime(futureTime);

        document.cookie = "isCheck=" + "Yes" + "; expires=" + dt;
      }
    },
  },
  computed: {
    ...mapState(["noLogin"]),
  },
  mounted() {
    this.loginStr = this.$store.state.loginStr;
    this.userEmail = this.$store.state.userEmail;

    // 读取本地储存
    this.userMsg = JSON.parse(localStorage.getItem("userMsg"));

    // console.log(obj);

    // 获取时间
    let dt = new Date();
    this.yy = dt.getFullYear();
    this.mm = dt.getMonth() + 1;
    this.dd = dt.getDate(); // 今天几号
    this.h = dt.getHours();
    this.m = dt.getMinutes();
    this.s = dt.getDay(); // 周几
    if (this.s == 1) {
      this.s = "一";
    } else if (this.s == 2) {
      this.s = "二";
    } else if (this.s == 3) {
      this.s = "三";
    } else if (this.s == 4) {
      this.s = "四";
    } else if (this.s == 5) {
      this.s = "五";
    } else if (this.s == 6) {
      this.s = "六";
    } else if (this.s == 71) {
      this.s = "日";
    }
  },
  beforeRouteLeave(to, from, next) {
    if (from.name == "Login") {
      this.$store.state.noLogin = false;
      this.$store.state.loginStr = "登录";
    }
    next();
  },
};
</script>

<style lang="less" scoped>
.webReg {
  width: 100%;
  .reg {
    width: 100%;
    display: flex;
    justify-content: space-between;
    .form {
      flex-grow: 1;
      padding-top: 20px;
      box-sizing: border-box;
      h3 {
        color: #808080;
        font-size: 18px;
        margin-bottom: 30px;
        &.active {
          color: #b03a16;
          font-weight: bold;
        }
      }
      div {
        margin-bottom: 12px;
        p {
          font-size: 14px;
          margin-bottom: 10px;
        }
        input {
          border: 1px solid #ccc;
          height: 35px;
          width: 215px;
          padding-left: 5px;
          font-size: 14px;
          &:hover {
            border-radius: 5px;
          }
        }
        input[type="submit"] {
          width: 120px;
          font-weight: bold;
          font-size: 16px;
          cursor: pointer;
        }
        .vcode {
          width: 80px;
          margin-right: 10px;
        }
        span {
          display: inline-block;
          border: 1px solid #ccc;
          color: #808080;
          font-size: 22px;
          font-weight: bold;
          border-radius: 5px;
          padding: 0 5px;
          vertical-align: middle;
          cursor: pointer;
          text-shadow: -1px 1px 5px #b54b00;
        }
      }
      .autoLogin {
        width: 220px;
        display: flex;
        padding-top: 5px;
        box-sizing: border-box;
        input {
          width: 20px;
          height: 20px;
          vertical-align: middle;
          cursor: pointer;
          margin-top: -2px;
          margin-right: 5px;
        }
        i {
          margin-right: 5px;
        }
        a {
          color: #34495e;
          font-size: 14px;
          margin-left: 5px;
        }
      }
    }
    .isHave {
      flex-basis: 270px;
      padding-top: 100px;
      box-sizing: border-box;
      .have {
        height: 100px;
        border-left: 1px solid #ccc;
        padding: 30px 0 0 20px;
        box-sizing: border-box;
        p {
          margin-bottom: 10px;
        }
        a {
          font-size: 14px;
          color: #808080;
        }
      }
    }
    .myTip {
      width: 100%;
      position: fixed;
      left: 50%;
      top: 70px;
      margin-left: -75px;
      .tip {
        p {
          width: 100%;
          position: absolute;
          top: 10px;
        }
      }
    }
  }
}
</style>